package Demo6;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class insert {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String str1 = scanner.nextLine();
        System.out.println("请输入密码");
        String str2 = scanner.nextLine();
        System.out.println("请输入级别");
        int str3 = scanner.nextInt();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=glxt", "sa", "sa123456");
            String sql = "insert into yh (yhmz,yhmm,yhjb) values ('?','?',?)";

            // 指令预处理
            PreparedStatement statement = connection.prepareStatement(sql);
            // ? 占位符
            // 设置占位符           占位符的序号 从1开始
            statement.setString(1,str1);
            statement.setString(1,str2);
            statement.setInt(1,str3);



            // 获取结果集
            int ret = statement.executeUpdate();
            System.out.println("被修改行数: " + ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
